package com.vmove.childtrain.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DateHandler {
	/**
	 * 把一个日期转换成指定格式的字符串
	 * @param time
	 * @return
	 */
	public static String dateToString(Date time) {
		String ctime = "";
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (time != null) {
			ctime = formatter.format(time);
		}
		return ctime;
	}
	// 把String转换成Date
	public static Date changeStringToDate(String time) throws ParseException {
		Date t = null;
		if (time != null && !time.trim().equals("")) {
			try {
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				t= formatter.parse(time);
			} catch (ParseException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		return t;
	}
	// 把String转换成Date如：2010-01-07 13:10:10,yyyy-MM-dd HH:mm:ss
	public static Date changeStringToDate(String time, String fomat) throws ParseException {
		Date t = null;
		if (time != null && !time.trim().equals("")) {
			try {
				SimpleDateFormat formatter = new SimpleDateFormat(fomat);
				t= formatter.parse(time);
			} catch (ParseException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		return t;
	}
	//把Date转换成String如：2010-01-07 13:10:10
	public static String dateToString(Date time, String fomat) {
		String ctime = "";
		SimpleDateFormat formatter = new SimpleDateFormat(fomat);
		if (time != null) {
			ctime = formatter.format(time);
		}
		return ctime;
	}

	public static String dateToYMDHMS(){
		return dateToString(new Date(), "yyMMddHHmmss");
	}

	public static String dateToYMD(){
		return dateToString(new Date(), "yyyy-MM-dd");
	}

	public static byte[] dateToYMDHMS_HEX() {
		SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd-HH-mm-ss");
		Date d = new Date();
		String s = formatter.format(d);
		String[] sa = s.split("-");
		byte[] res = new byte[6];
		res[0] = ByteUtil.getBytes(Integer.parseInt(sa[0]))[3];
		res[1] = ByteUtil.getBytes(Integer.parseInt(sa[1]))[3];
		res[2] = ByteUtil.getBytes(Integer.parseInt(sa[2]))[3];
		res[3] = ByteUtil.getBytes(Integer.parseInt(sa[3]))[3];
		res[4] = ByteUtil.getBytes(Integer.parseInt(sa[4]))[3];
		res[5] = ByteUtil.getBytes(Integer.parseInt(sa[5]))[3];
		return res;
	}
 
}